package com.bw.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.bw.entity.Dict;
import com.bw.entity.Employee;
import com.bw.entity.EmployeeAdd;
import com.bw.entity.EmployeeQuery;
import com.bw.mapper.EmployeeMapper;
import com.bw.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    EmployeeService employeeService;

    /**
     * 列表方法
     *
     * @return
     */
    @RequestMapping("/list.do")
    public String list(EmployeeQuery query, Integer pageNum, Model model) {
        // 渲染下拉框

        List<Dict> deptList = employeeMapper.listDept();
        model.addAttribute("deptList", deptList);
        List<Dict> groupList = employeeMapper.listGroup();
        model.addAttribute("groupList", groupList);
        //分页查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);
        List<Employee> list = employeeMapper.selectByParam(query);
        PageInfo<Employee> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        // 查询条件不丢失
        model.addAttribute("query",query);
        return "list";
    }

    @RequestMapping("/del.do")
    public  String del(String id){
        employeeMapper.del(id);
        return "redirect:list.do";
    }



    @RequestMapping("/addPage.do")
    public  String addPage(Model model){
        // 渲染下拉框
        List<Dict> deptList = employeeMapper.listDept();
        model.addAttribute("deptList", deptList);
        List<Dict> groupList = employeeMapper.listGroup();
        model.addAttribute("groupList", groupList);

        return "add";
    }



    @RequestMapping("/add.do")
    public  String add(@Valid EmployeeAdd employeeAdd, BindingResult result,Model model){
        if (result.hasErrors()){

            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            // 渲染下拉框
            List<Dict> deptList = employeeMapper.listDept();
            model.addAttribute("deptList", deptList);
            List<Dict> groupList = employeeMapper.listGroup();
            model.addAttribute("groupList", groupList);
            return "add";
        }
        //员工工号以“GF”开始
        if (!employeeAdd.getNum().startsWith("GF")){
            model.addAttribute("error","员工工号必须以“GF”开始");
            // 渲染下拉框
            List<Dict> deptList = employeeMapper.listDept();
            model.addAttribute("deptList", deptList);
            List<Dict> groupList = employeeMapper.listGroup();
            model.addAttribute("groupList", groupList);
            return "add";
        }
        // ，工号不能重复
        Employee employeeFromDB = employeeMapper.selectByNum(employeeAdd.getNum());
        if (employeeFromDB != null){
            model.addAttribute("error","工号不能重复");
            // 渲染下拉框
            List<Dict> deptList = employeeMapper.listDept();
            model.addAttribute("deptList", deptList);
            List<Dict> groupList = employeeMapper.listGroup();
            model.addAttribute("groupList", groupList);
            return "add";
        }
        //入职时间小于当前时间
        String createTimeStr = employeeAdd.getCreateTime();
        DateTime createTime = DateUtil.parse(createTimeStr, "yyyy-MM-dd");

        if (createTime.isAfter(new Date())){
            model.addAttribute("error","入职时间小于当前时间");
            // 渲染下拉框
            List<Dict> deptList = employeeMapper.listDept();
            model.addAttribute("deptList", deptList);
            List<Dict> groupList = employeeMapper.listGroup();
            model.addAttribute("groupList", groupList);
            return "add";

        }

        employeeService.add(employeeAdd);
        return "redirect:list.do";
    }
}
